package chapterSystem.client.interfaceDrawing.activities;

import chapterSystem.client.ChapterSystemService;
import chapterSystem.client.callbacks.LoggCallback;
import chapterSystem.client.entities.Pair;
import chapterSystem.client.model.entitiesInt.Brother;
import chapterSystem.client.model.entitiesInt.Order;
import chapterSystem.client.model.enums.ActivityType;
import com.google.gwt.dom.client.Style;
import com.google.gwt.event.dom.client.ClickEvent;
import com.google.gwt.event.dom.client.ClickHandler;
import com.google.gwt.user.client.DOM;
import com.google.gwt.user.client.rpc.AsyncCallback;
import com.google.gwt.user.client.ui.*;

import java.util.List;

/**
 * User: Hasp
 * Date: 07.04.12
 * Time: 20:45
 */
public class OrdersActivity extends Activity {
    public OrdersActivity(final Brother brother) {
        super(brother, ActivityType.ORDERS);
    }

    @Override
    public void drawActivity(StackLayoutPanel workArea) {
        ChapterSystemService.App.getInstance().getOrdersById(brother.getId(), new OrdersCallback(workArea));
        ChapterSystemService.App.getInstance().addLog(brother.getId(), id, "Review orders",
                            new LoggCallback());
    }


    private class OrdersCallback implements AsyncCallback<Pair<List<Order>, List<Order>>> {
        final private StackLayoutPanel workArea;

        private OrdersCallback(StackLayoutPanel workArea) {
            this.workArea = workArea;
        }

        @Override
        public void onFailure(Throwable caught) {
            workArea.clear();
            workArea.add(new HTML(), new HTML("Failed to connect to server"), 4);
        }

        @Override
        public void onSuccess(Pair<List<Order>, List<Order>> result) {
            workArea.clear();
            final HTML myOrdersTitle = new HTML("Submitted Orders");
            final HTML ordersToMeTitle = new HTML("Orders To Me");

            final HTML noOrdersHeader = new HTML("You have no orders");
            if (result == null) {
                workArea.add(noOrdersHeader, ordersToMeTitle, 4);
                workArea.add(noOrdersHeader, myOrdersTitle, 4);

                return;
            }

            final StackLayoutPanel myOrders = new StackLayoutPanel(Style.Unit.MM);
            final VerticalPanel myOrdersExecuted = new VerticalPanel();
            final VerticalPanel myOrdersOpened = new VerticalPanel();

            final StackLayoutPanel ordersToMe = new StackLayoutPanel(Style.Unit.MM);
            final VerticalPanel ordersToMeExecuted = new VerticalPanel();
            final VerticalPanel ordersToMeOpened = new VerticalPanel();


            if (result.first.size() > 0) {
                myOrdersExecuted.setWidth("100%");
                myOrdersOpened.setWidth("100%");
                myOrdersOpened.setBorderWidth(3);
                myOrdersExecuted.setBorderWidth(3);
                myOrders.add(new ScrollPanel(myOrdersExecuted), new HTML("Executed orders"), 12);
                myOrders.add(new ScrollPanel(myOrdersOpened), new HTML("Active orders"), 12);

                for (final Order order : result.first) {
                    if (order.isExecuted()) {
                        myOrdersExecuted.add(formatOrderHTML(order));
                    } else {
                        final VerticalPanel panel = new VerticalPanel();
                        final Button editButton = new Button("Edit order");
                        editButton.addClickHandler(new ClickHandler() {
                            @Override
                            public void onClick(ClickEvent event) {
                                new EditBattleOrderActivity(brother, ActivityType.EDIT_ORDER, OrdersActivity.this,
                                        new Pair<String, Integer>(order.getReceiverName(), order.getReceiverId()),
                                        order).drawActivity(workArea);
                            }
                        });

                        panel.setWidth("100%");
//                        panel.setBorderWidth(3);
                        DOM.setStyleAttribute(panel.getElement(), "background", "#ddd");
                        DOM.setStyleAttribute(panel.getElement(), "border", "black");
                        panel.add(formatOrderHTML(order));
                        panel.add(editButton);
                        panel.setCellHorizontalAlignment(editButton, HasAlignment.ALIGN_CENTER);

                        myOrdersOpened.add(panel);
                    }
                }

                workArea.add(myOrders, myOrdersTitle, 4);
            } else {
                workArea.add(noOrdersHeader, myOrdersTitle, 4);
            }

            if (result.second.size() > 0) {
                ordersToMeOpened.setWidth("100%");
                ordersToMeExecuted.setWidth("100%");
                ordersToMeOpened.setBorderWidth(3);
                ordersToMeExecuted.setBorderWidth(3);

                ordersToMe.add(new ScrollPanel(ordersToMeExecuted), new HTML("Executed orders"), 12);
                ordersToMe.add(new ScrollPanel(ordersToMeOpened), new HTML("Active orders"), 12);

                for (final Order order : result.second) {
                    final VerticalPanel panel = new VerticalPanel();
                    panel.setWidth("100%");
                    //panel.setBorderWidth(3);
                    DOM.setStyleAttribute(panel.getElement(), "background", "#ddd");
                    DOM.setStyleAttribute(panel.getElement(), "border", "black");

                    if (order.isExecuted()) {
                        final Button editExecutionReportButton = new Button("Edit execution report");
                        editExecutionReportButton.addClickHandler(new ClickHandler() {
                            @Override
                            public void onClick(ClickEvent event) {
                                new EditOrderExecutionActivity(brother, ActivityType.EDIT_EXECUTION, order,
                                        OrdersActivity.this).drawActivity(workArea);

                            }
                        });

                        panel.add(formatOrderHTML(order));
                        panel.add(editExecutionReportButton);
                        panel.setCellHorizontalAlignment(editExecutionReportButton, HasAlignment.ALIGN_CENTER);

                        ordersToMeExecuted.add(panel);
                    } else {
                        final Button writeExecutionButton = new Button("Write Execution Report");
                        writeExecutionButton.addClickHandler(new ClickHandler() {
                            @Override
                            public void onClick(ClickEvent event) {
                                new WriteOrderExecutionActivity(brother, ActivityType.WRITE_EXECUTION, order,
                                        OrdersActivity.this).drawActivity(workArea);
                            }
                        });

                        panel.add(formatOrderHTML(order));
                        panel.add(writeExecutionButton);
                        panel.setCellHorizontalAlignment(writeExecutionButton, HasAlignment.ALIGN_CENTER);

                        ordersToMeOpened.add(panel);
                    }
                }

                workArea.add(ordersToMe, ordersToMeTitle, 4);
            } else {
                workArea.add(noOrdersHeader, ordersToMeTitle, 4);
            }
        }

        private HTML formatOrderHTML(Order order) {
            final String header = "<h3>From: " + order.getAuthorName() + " <br/>To: " +
                    order.getReceiverName() + "<br/>Date: " + order.getDate().toString() + "</h3><br/><p/>";
            final StringBuilder orderText = new StringBuilder(order.getText()).append("<br/> Is Executed: ").
                    append(order.getExecutionDate() == null ? "No" : "Yes,  " + order.getExecutionDate().toString()).
                    append("<p/>");
            if (order.isExecuted()) {
                orderText.append("Execution report: <br/>");
                orderText.append(order.getExecutionReport()).append("<p/>");
            }

            final HTML html = new HTML(header + orderText.toString());
            DOM.setStyleAttribute(html.getElement(), "background", "#ddd");
            DOM.setStyleAttribute(html.getElement(), "backgroundImage", "_");
            return html;
        }
    }
}
